﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;

public partial class Hospital_HospitalTimeStatistics : BasePage
{
    public Hospital_HospitalTimeStatistics()
    { }
    protected void Page_Load(object sender, EventArgs e)
    {
        QueryControl1.OnQueryClick += new EventHandler(QueryControl1_OnQueryClick);
        if (!this.IsPostBack)
        {
            ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
            QueryControl1.BindDataPre((SuperviseQuery)ViewState["query"]);//绑定查询条件的一些数据源 质量层次等
            BindData();
            QueryControl1.BindSort(gvwInfo);
        }
    }

    void QueryControl1_OnQueryClick(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        ViewState["query"] = QueryControl1.UpdateQueryFromValue((SuperviseQuery)ViewState["query"]);
        BindData();
    }

    //绑定数据源
    private void BindData()
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            //分组字段
            GroupField = GroupByField.PurchaseTimeYear,
            //查询条件
            Query = (SuperviseQuery)ViewState["query"],
            //分组查询字段
            GroupSelectField = (ShowColumns.ShowColumn == null) ? GroupFields.PurchaseCountTotal | GroupFields.PurchaseAmountTotal | GroupFields.WareHouseCountTotal | GroupFields.WareHouseAmountTotal : ShowColumns.ShowColumn.GroupFields,
            //排序字段
            SortField = QueryControl1.SortField == "" ? "PurchaseTimeYear ASC" : QueryControl1.SortField
        };

        //将数据填入gridview中
        System.Data.DataTable dt = ie.ExecutePage(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);

        AspNetPager1.RecordCount = t;

        //显示相应的字段
        GridViewUtil.AdjustGridView(gvwInfo, dt);

        gvwInfo.DataSource = dt;
        gvwInfo.DataBind();
        setChart(dt);
        SetColumnVisible();

    }

    /// <summary>
    /// 屏蔽相关查询连接
    /// </summary>
    private void SetColumnVisible()
    {

        if (!string.IsNullOrEmpty(QueryControl1.CompanyID_PS))
        {
            foreach (GridViewRow row in gvwInfo.Rows)
            {
                row.FindControl("lbnCom_PS").Visible = false;
            }
        }
        if (!string.IsNullOrEmpty(QueryControl1.HospitalID))
        {
            foreach (GridViewRow row in gvwInfo.Rows)
            {
                row.FindControl("lbnHos").Visible = false;
            }
        }
    }



    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindData();
    }
    protected void lbnSwitch_Click(object sender, EventArgs e)
    {
        //if (lbnSwitch.CommandArgument == "simple")
        //{
        //    ltlAdvance.Text = "";
        //    lbnSwitch.CommandArgument = "advance";
        //    lbnSwitch.Text = "简单查询";
        //}
        //else
        //{
        //    ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
        //    lbnSwitch.CommandArgument = "simple";
        //    lbnSwitch.Text = "高级查询";
        //}
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }


    protected void gvwInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Hospital")
        {
            //设置分组查询字段GroupSelectField
            //if (ShowColumns.ShowColumn == null)
            //{
            //    ShowColumns.ShowColumn = new ShowColumns();
            //}
            //ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PurchaseCountTotal | GroupFields.WareHouseCountTotal;

            string sessionid = Guid.NewGuid().ToString();
            DateTime StartTime = DateTime.Parse(e.CommandArgument.ToString() + "-01-01");
            DateTime EndTime = DateTime.Parse(e.CommandArgument.ToString() + "-12-31");
            //DateTime StartTime = DateTime.Parse("2008-01-01");
            //DateTime EndTime = DateTime.Parse("2008-12-31");
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.StartTimeFrom = StartTime;
            query.StartTimeTo = EndTime;
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["HospitalID"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }

        if (e.CommandName == "Company")
        {
            string sessionid = Guid.NewGuid().ToString();
            DateTime StartTime = DateTime.Parse(e.CommandArgument.ToString() + "-01-01");
            DateTime EndTime = DateTime.Parse(e.CommandArgument.ToString() + "-12-31");
            //DateTime StartTime = DateTime.Parse("2008-01-01");
            //DateTime EndTime = DateTime.Parse("2008-12-31");
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.StartTimeFrom = StartTime;
            query.StartTimeTo = EndTime;
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["CompanyID_PS"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }

        if (e.CommandName == "Drugs")
        {
            string sessionid = Guid.NewGuid().ToString();
            DateTime StartTime = DateTime.Parse(e.CommandArgument.ToString() + "-01-01");
            DateTime EndTime = DateTime.Parse(e.CommandArgument.ToString() + "-12-31");
            //DateTime StartTime = DateTime.Parse("2008-01-01");
            //DateTime EndTime = DateTime.Parse("2008-12-31");
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.StartTimeFrom = StartTime;
            query.StartTimeTo = EndTime;
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["ProcureCatalogID"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }

    protected void ddlOther_SelectedIndexChanged(object sender, EventArgs e)
    {
        string stkey = "";
        string sessionid = string.Empty;
        GridViewRow row = (GridViewRow)((Control)sender).Parent.Parent;
        DropDownList ddl = (DropDownList)row.FindControl("ddlOther");
        stkey = ddl.SelectedValue;

        if (stkey != "")
        {
            sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            //赋予当前行的主键值
            query.StartTimeFrom = DateTime.Parse(row.Cells[1].Text + "-01-01");
            query.StartTimeTo = DateTime.Parse(row.Cells[1].Text + "-12-31");
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings[stkey];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }

    /// <summary>
    /// 设置图表
    /// </summary>
    /// <param name="?"></param>
    private void setChart(System.Data.DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            this.ChartControl1.Visible = false;
            HZ.Supervise.Web.WebService.ChartConfig serChart = new HZ.Supervise.Web.WebService.ChartConfig();
            dt.TableName = "年统计";
            serChart.DataSource = dt;
            serChart.XName = "PurchaseTimeYear";
            serChart.YNames = "PurchaseAmountTotal";

            if (ShowColumns.ShowColumn == null)
            {
                serChart.XName = "PurchaseTimeYear";
                serChart.YNames = "PurchaseAmountTotal";
            }
            else
            {
                string[] _columns = ShowColumns.ShowColumn.GroupFields.ToString().Split(',');
                serChart.XName = "PurchaseTimeYear";
                serChart.YNames = _columns[0];
            }
            serChart.ChartTypes = HZ.Supervise.Web.WebService.ChartType.ColumnChart;

            ChartControl1.webChart = serChart;
            ChartControl1.PageId = Guid.NewGuid().ToString();
            ChartControl1.EntranceChart();
        }
        else
        {
            ChartControl1.Visible = false;
        }
    }
    protected void lbtnSee_Click(object sender, EventArgs e)
    {
        if (this.lbtnSee.Text == "显示图表")
        {
            ChartControl1.Visible = true;
            this.lbtnSee.Text = "隐藏图表";
        }
        else
        {
            ChartControl1.Visible = false;
            this.lbtnSee.Text = "显示图表";
        }
    }
}
